package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Role;
import cn.itsource.system.mapper.RoleMapper;
import cn.itsource.system.service.IRoleService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zj
 * @since 2022-12-09
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        //先添加角色表 再维护关联表
        roleMapper.save(role);
        handleRelationShip(role);
    }


    /*
    * 维护角色菜单 和角色权限中间表
    * */
    private void handleRelationShip(Role role) {
        roleMapper.removeRoleMenu(role.getId());
        roleMapper.removeRolePermission(role.getId());
        roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        roleMapper.saveRolePermission(role.getId(),role.getPermissions());
    }

}
